Comparison to Xarray


Imports

import matplotlib.pyplot as plt
import uxarray as ux
import xarray as xr

Data

Structured

base_path = "../../meshfiles/"
ds_path = base_path + "outCSne30.structured.nc"
xrds = xr.open_dataset(ds_path)
xrds
<xarray.Dataset> Size: 30kB
Dimensions:  (lat: 45, lon: 80)
Coordinates:
  * lat      (lat) int64 360B -90 -86 -82 -78 -74 -70 -66 ... 66 70 74 78 82 86
  * lon      (lon) float64 640B -180.0 -175.5 -171.0 ... 166.5 171.0 175.5
Data variables:
    psi      (lat, lon) float64 29kB ...

Unstructured

base_path = "../../meshfiles/"
grid_filename = base_path + "outCSne30.grid.ug"
data_filename = base_path + "outCSne30.data.nc"
uxds = ux.open_dataset(grid_filename, data_filename)
uxds
<xarray.UxDataset> Size: 43kB
Dimensions:  (n_face: 5400)
Dimensions without coordinates: n_face
Data variables:
    psi      (n_face) float64 43kB 1.351 1.331 1.31 1.289 ... 0.6909 0.67 0.6495

Visualization

Xarray

xrds["psi"].plot(figsize=(10, 5), cmap="inferno")
<matplotlib.collections.QuadMesh at 0x7f7abfd5e1a0>
../../_images/f77d39ac3d641b3fe86d240d0773383088a12e06905decb4d88778bf8578f5b3.png
fig, axs = plt.subplots(ncols=2, figsize=(20, 5))

xrds["psi"].plot(cmap="inferno", ax=axs[0])
xrds["psi"].plot(cmap="cividis", ax=axs[1])
<matplotlib.collections.QuadMesh at 0x7f7abeba06d0>
../../_images/2a980190cb2b42de960715510b756ed27c9f331eb09920184264caac2997032e.png

UXarray

todo

uxds["psi"].plot(width=1000, height=500, cmap="inferno")

Since Xarray’s plotting functionality is written using Matplotlib, we’ll switch to the matplotlib backend in UXarray to get more similar results.

uxds["psi"].plot(aspect=2, fig_inches=14, cmap="inferno", backend="matplotlib")
(
    uxds["psi"].plot(aspect=2, fig_size=600, cmap="inferno", backend="matplotlib", linewidth=0)
    + uxds["psi"].plot(aspect=2, fig_size=600, cmap="cividis", backend="matplotlib", linewidth=0)
).opts(fig_size=300)

Xarray with hvPlot

One of the primary drawbacks to UXarray’s use of HoloViews for visualization is that there is no direct way to integrate plots generated with Xarray and UXarray together. This can be alleviated by using the hvPlot library, specifically hvplot.xarray, on Xarray’s data structures.

import holoviews as hv
import hvplot.xarray

hv.extension("bokeh")

By using xrds.hvplot() as opposed to xrds.plot(), we can create a simple figure showcasing our Structured Grid figure from Xarray and Unstructured Grid figure from UXarray in a single plot.

(
    xrds.hvplot(cmap="inferno", title="Xarray with hvPlot", width=500, height=250)
    + uxds["psi"].plot(cmap="inferno", backend="bokeh", title="UXarray Plot", width=500, height=250)
)

If we are not interested in the outlines of each polygon in our Unstructured Grid, we can create a raster plot, which will better match the result produced by Xarray.

ADD NOTE ABOUT POLYGON SECTION

(
    xrds.hvplot(cmap="inferno", title="Xarray with hvPlot", width=500, height=250)
    + uxds["psi"].plot.rasterize(method='polygon', cmap="inferno", backend="bokeh", title="UXarray Plot", width=500, height=250)
)

In addition to using hvPlot, the user could also use HoloViews or Datashader on Xarray’s data structures, but this cookbook will not go into detail.

ADD LINK TO EXAMPLES?